package com.atguigu.ggkt.activity.api;

import com.atguigu.ggkt.activity.mapper.CouponInfoMapper;
import com.atguigu.ggkt.activity.service.CouponInfoService;
import com.atguigu.ggkt.activity.service.CouponUseService;
import com.atguigu.ggkt.model.activity.CouponInfo;
import com.atguigu.ggkt.model.activity.CouponUse;
import com.atguigu.ggkt.model.vod.Course;
import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.utils.AuthContextHolder;
import com.atguigu.ggkt.vo.activity.CouponUseQueryVo;
import com.atguigu.ggkt.vo.vod.CourseQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "优惠券接口")
@RestController
@RequestMapping("/api/activity/couponInfo")
public class CouponInfoApiController {

    @Autowired
    private CouponInfoMapper couponInfoMapper;
    @Autowired
    private CouponUseService couponUseService;

    //根据优惠券id查询
    @ApiOperation(value = "获取优惠券")
    @GetMapping(value = "inner/getById/{couponId}")
    public CouponInfo getById(@PathVariable("couponId") Long couponId) {
        CouponInfo couponInfo = couponInfoMapper.selectById(couponId);
        return couponInfo;
    }

    //更新优惠券
    @ApiOperation(value = "更新优惠券使用状态")
    @GetMapping(value = "inner/updateCouponInfoUseStatus/{couponUseId}/{orderId}")
    public Boolean updateCouponInfoUseStatus(@PathVariable("couponUseId") Long couponUseId, @PathVariable("orderId") String orderId) {
        couponUseService.updateCouponInfoUseStatus(couponUseId, orderId);
        return true;
    }

    @ApiOperation("查询用户优惠券列表")
    @GetMapping("use/{page}/{limit}")
    public Result findPageCouponUse(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        System.out.println("s1 >>> /api/activity/couponInfo/use/"+page+"/"+limit);
        //创建page对象
        Long userId = AuthContextHolder.getUserId();
        Page<CouponUse> pageParam = new Page<>(page,limit);
        Map<String,Object> map = couponUseService.findPage(pageParam, userId);
        return Result.ok(map);
    }

    @ApiOperation("查询用户优惠券列表")
    @GetMapping("findCouponInfo")
    public Result findCouponInfo() {
        Long userId = AuthContextHolder.getUserId();
        System.out.println("s1 >>> /findCouponInfo, "+userId);

        //创建page对象
        Page<CouponUse> pageParam = new Page<>();
        Map<String,Object> map = couponUseService.selectCouponUse(pageParam, userId);
        return Result.ok(map);
    }
}
